<?php
namespace Customer\Controller;

use Wechat\Controller\WxCustomerBaseController;

class IndexController extends WxCustomerBaseController {
	
	protected $customerModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->customerModel = D("Customer/Customer");
	}

    // 会员中心首页
	public function center() {
		$customer = $this->customer;
		$this->assign("customer", $customer);
		$this->display(':center');
		return;
    }

    // 会员中心内容主页
	public function main() {
		$this->display(':main');
		return;
    }

    // 会员信息
	public function info() {
		$customer = $this->customer;
		$this->assign("customer", $customer);
		$this->display(':info');
		return;
    }

    // 会员编辑
	public function edit() {
		$customer = $this->customer;
		$this->assign("customer", $customer);
		$this->display(':edit');
		return;
    }
    
    // 会员编辑
    public function doEdit() {
		$id = $_POST['id'];
		
    	////// validate
    	$rules = array(
    			// array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
    			array('real_name', 'require', '姓名不能为空！'),
    			array('sex', 'require', '性别不能为空！'),
				array('email', 'require', '邮箱不能为空！', 0, 'regex', 3),						// 验证email字段是否为空
				array('email', 'email', '邮箱格式不正确！', 0, '', 3),							// 验证email字段格式是否正确
				array('email', '', '邮箱帐号已经存在！', 0, 'unique', 3),							// 验证email字段是否唯一
    	);
    	if ($this->customerModel->validate($rules)->create() === false) {
    		$this->error($this->customerModel->getError());
            return;
    	}
    	
		////// update
		$customer['avatar'] = I('post.avatar');
		$customer['real_name'] = I('post.real_name');
		$customer['sex'] = I('post.sex');
		$customer['email'] = I('post.email');
// 		$customer['status'] = 3;										// 等等认证中...
		$customer['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->customerModel->where(array("id" => $id))->save($customer);
		if (false === $result) {
    		$this->error("提交失败，请联系管理员处理！");
            return;
		}

		$updatedCustomer = $this->customerModel->where(array("id" => $id))->find();
		$this->success("提交成功！");
		return;
    }

    /*************************************** wx **************************************************/
    // 会员编辑
    public function doWxEdit() {
		$id = $_POST['id'];

		////// validate
		$rules = array(
    		// array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
    		array('real_name', 'require', '姓名不能为空！', 1),
    		array('tel', 'require', '手机号不能为空！', 1),
//     		array('tel', '', '手机号已被注册！！', 0, 'unique', 3),
		);
		if ($this->customerModel->validate($rules)->create() === false) {
		    $data['code'] = 499;
		    $data['msg'] = '编辑失败，' . $this->customerModel->getError();
		    $this->ajaxReturn($data);
		}
    	
		////// update
        $customer['real_name'] = I('post.real_name');
	    $customer['tel'] = I('post.tel');
	    $customer['avatar'] = I('post.avatar');
	    $customer['sex'] = I('post.sex');
		$customer['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->customerModel->where(array("id" => $id))->save($customer);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '提交失败，请联系管理员处理！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $customer;
		$this->ajaxReturn($data);
    }
    
}
